﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TypeWrite.Core;
using TypeWrite.Data;
using WebMatrix.WebData;

namespace TypeWrite.MVC4.Controllers
{
    [Authorize(Roles = Roles.AdminRoleName)]
    public class BloggersController : Controller
    {
        TypeWriteDataContext db = ContextFactory.CreateInstance(string.Empty);

        //
        // GET: /Bloggers/

        public ActionResult Index()
        {
            return View(db.Bloggers.Where(b => !b.Deleted.HasValue));
        }

        //
        // GET: /Bloggers/Details/5

        public ActionResult Details(int id)
        {
            Blogger blogger = db.Bloggers.SingleOrDefault(b => b.PK == id);

            if (blogger == null)
                return RedirectToAction("UnrecognizedBlogger", new { id = id });
            else
                return View(blogger);
        }

        //
        // GET: /Bloggers/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Bloggers/Create

        [HttpPost]
        public ActionResult Create(TypeWrite.MVC4.Models.Blogger model)
        {
            try
            {
                WebSecurity.CreateUserAndAccount(model.Username, model.Password);

                Data.User user = db.Users.SingleOrDefault(u => u.Username == model.Username);

                Blogger blogger = new Blogger();

                blogger.Created = DateTime.Now;
                blogger.DisplayName = model.Username;
                blogger.Updated = blogger.Created;
                blogger.UserId = user.UserId;

                db.Bloggers.InsertOnSubmit(blogger);

                db.SubmitChanges();

                Data.Profile profile = new Profile();

                profile.BloggerID = blogger.PK;
                profile.Email = model.Email;
                profile.Updated = blogger.Created;

                db.Profiles.InsertOnSubmit(profile);

                db.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View("Error", new HandleErrorInfo(ex, "Bloggers", "Create"));
            }
        }

        //
        // GET: /Bloggers/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Bloggers/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Bloggers/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Bloggers/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult UnrecognizedBlogger(int id)
        {
            return View(id);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            db.Dispose();
        }

    }
}
